﻿var p = [];
var total_price = 0;
var _top = 0;
$(function () {
    $('#nav').height($(window).height() - 168 - 50);
    _top = $('#navmenu').offset().top;
    $(window).scroll(function () {
        if ($(window).scrollTop() >= _top) {
            $('#navmenu').css({
                'position': 'fixed',
                'left': '0px',
                'top': '0px',
                'z-index': 10
            });

            $('.nav-lf').css({
                'position': 'fixed',
                'left': '10px',
                'top': '46px',
                'z-index': 10
            });

            $('#nav').height($(window).height() - 46 - 50);
        }
        else {
            $('#navmenu').css({
                'position': 'initial',
                'left': 'initial',
                'top': 'initial',
                'z-index': 10
            });

            $('.nav-lf').css({
                'position': 'absolute',
                'left': '10px',
                'top': 'initial',
                'z-index': 10
            });

            $('#nav').height($(window).height() - 168 - 50);
            $('#nav').scrollTop(0);
        }
    });
    var s = '';
    for (var i = 0; i < ca.length; i++) {
        if (sprod(ca[i].id)) {
            $('#nav').append('<li style="width:90px;"><a href="#' + ca[i].id + '" style="color:#000;font-weight:bold;">' + ca[i].name + '</a><bb id="cb_' + ca[i].code + '" style="display: none;position: absolute;top: 0.05rem;right: 0.05rem;width: 0.2rem;height: 0.2rem;line-height: 0.2rem;font-size: 10px;color: #fff;background-color: #FF5151;border-radius: 0.1rem;text-align:center;">0</bb></li>');
            s += '<div style="padding-top:10px;padding-left:10px;color:#666;font-size:13px;"><a name="' + ca[i].id + '"></a>' + ca[i].name + '</div><div class="section">';
            for (var n = 0; n < prod.length; n++) {
                if (prod[n].cid == ca[i].id) {
                    s += product(prod[n], n);
                }
            }
            s += '</div>';
        }
    }
    $('#container').html(s);
    $('footer').html('<div class="ft-lt" onclick="showcart()" style="width:58%;"><p>合计：<span id="total" class="total">￥0.00元</span>　起订￥' + price_startorder + '</p></div><div class="ft-rt" style="margin-right:1px;"><p onclick="location.href=\'/my/rights\';">返回</p></div><div class="ft-rt" style="background:#0094ff;"><p onclick="startorder()">选好了</p></div>');
    $('footer').css('top', ($(window).height() - 45) + 'px');
    $('footer').css('width', $(window).width() + 'px');
    $('footer').css('overflow', 'hidden');
    $('#container').css('width', ($(window).width() - $('#nav').width() - 0) + 'px');
    total_price = 0;
    for (var i = 0; i < cart.length; i++) {
        total_price += cart[i].pprice * cart[i].num;
    }
    $('#total').html('￥' + total_price.toFixed(2) + '元');
});
function sprod(cid) {
    for (var i = 0; i < prod.length; i++) {
        if(prod[i].cid == cid){
            return true;
        }
    }
    return false;
}
function product(d, tt) {
    return '<div class="prt-lt" style="width:' + ($(window).width() - $('#nav').width() - 0) + 'px;"><div class="lt-lt"><a href="/shop/upgrade_product_dtl?code=' + d.code + '&t=' + d.t + '&dcode=' + d.dcode + '&x=' + x + '&price_startorder=' + price_startorder + '"><img src="' + d.pic + '"></a></div><div class="lt-ct" style="width:70%;"><a href="/shop/upgrade_product_dtl?code=' + d.code + '&t=' + d.t + '&dcode=' + d.dcode + '&x=' + x + '&price_startorder=' + price_startorder + '"><p>' + d.name + '</p><p class="desc">' + d.subname + '</p><p class="desc">评论：' + d.comment_num + '</p><p class="pr"><span style="text-decoration:line-through;color:#888;font-weight:normal;">￥' + d.price + '</span> <span class="price">￥' + d.price_buy + '</span></p></a><p>' + cartnum(d.code) + '<a href="/shop/upgrade_product_dtl?code=' + d.code + '&t=' + d.t + '&dcode=' + d.dcode + '&x=' + x + '&price_startorder=' + price_startorder + '" style="border:none;width:60px;height:24px;line-height:24px;text-align:center;background:#FFB700;color:#000;font-size:13px;border-radius:8px;display:block;float:right;margin-top:10px;">去购买</a></p></div><div style="clear:both;height:10px;">&nbsp;</div></div>';
}
//function showp(n) {
//    layer.open({
//        content: '<div style="height:250px;overflow-y:auto;"><img src="' + (prod[n].pic == '' ? '/ui/d.jpg' : prod[n].pic) + '" onerror="pic404(this)" style="max-height:100px;"><br><b>' + prod[n].name + '</b><div style="text-align:left;">' + prod[n].descs + '</div></div><br><br><a href="javascript:;" onclick="layer.closeAll()">关闭</a>'
//    });
//}
function cartnum(c) {
    for (var i = 0; i < cart.length; i++) {
        if (cart[i].pcode == c) {
            return '<p><span style="font-size:12px;color:#fff;background:#f60;padding:3px 10px;border-radius:11px;">已选 ' + cart[i].g[cart[i].gg - 1].name + '</span><span style="float:right;"><img src="/ui/shop/images/add2.png" style="width:16px;height:16px;vertical-align: text-bottom;" onclick="reducew(' + i + ')"> ' + cart[i].num + ' <img src="/ui/shop/images/add.png" style="width:16px;height:16px;vertical-align: text-bottom;" onclick="plusw(' + i + ')"></span></p>';
        }
    }
    return '';
}
function startorder() {
    if (total_price < price_startorder) {
        txttip('起订金额' + price_startorder + '，您还差' + (price_startorder - total_price).toFixed(2));
        return;
    }
    loading();
    location.href = '/shop/cart_upgrade?x=' + x + '&price_start=' + price_startorder;
}
function reducew(n) {
    if (cart[n].num <= 1) {
        layer.open({
            content: '确认删除？'
    , btn: ['删除', '取消']
    , skin: 'footer'
    , yes: function (index) {
        loading();
        $.post('/dos_m/shop_cart_upgrade_del', { code: cart[n].code, x: x }, function (d) { eval(d); });
    }
        });
    }
    else {
        loading();
        $.post('/dos_m/shop_cart_upgrade_reducew', { code: cart[n].code, x: x }, function (d) { eval(d); });
    }
}
function plusw(n) {
    loading();
    $.post('/dos_m/shop_cart_upgrade_plusw', { code: cart[n].code, x: x }, function (d) { eval(d); });
}
function ok() {
    location.href = location.href;
}